package ro.bluecube.admlocal.registruagricol.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import ro.bluecube.admlocal.registruagricol.RegistruAgricolServiceLocal;
import ro.bluecube.admlocal.registruagricol.RegistruAgricolServiceRemote;
import ro.bluecube.admlocal.registruagricol.entity.Localitate;
import ro.bluecube.admlocal.registruagricol.entity.Titular;
import ro.bluecube.admlocal.registruagricol.entity.Volum;

@Stateless
public class RegistruAgricolServiceImpl implements
		RegistruAgricolServiceRemote, RegistruAgricolServiceLocal {

	@PersistenceContext
	EntityManager em;
	
	@Override
	public List<Localitate> getAllLocalitate() {
		return new ArrayList<Localitate>(em.createNamedQuery("findAll").getResultList());
	}

	@Override
	public List<Localitate> getLocalitateByDenumire(String denumire) {
		Query query = em.createNamedQuery("findByDenumire");
		query.setParameter(1, denumire);
		return new ArrayList<Localitate>(query.getResultList());
	}

	@Override
	public Localitate getLocalitateById(Long id) {
		Query query = em.createNamedQuery("findById");
		query.setParameter(1, id);
		return (Localitate) query.getSingleResult();
	}

	@Override
	public Localitate add(Localitate localitate) {
		return null;
	}

	@Override
	public void remove(Localitate localitate) {
	}

	@Override
	public Localitate update(Localitate localitate) {
		return null;
	}

	
	
	@Override
	public Volum add(Volum volum) {
		em.persist(volum);
		return getVolumById(volum.getId() != null ? volum.getId() : 0L);
	}

	@Override
	public Titular add(Titular titular) {
		em.persist(titular);
		return getTitularById(titular.getId() != null ? titular.getId() : 0L);
	}

	@Override
	public List<Titular> getAllTitular() {
		return em.createNamedQuery("findAllTitular").getResultList();
	}

	@Override
	public List<Volum> getAllVolum() {
		return em.createNamedQuery("findAllVolum").getResultList();
	}

	@Override
	public Titular getTitularById(Long id) {
		Query query = em.createNamedQuery("findTitularById");
		query.setParameter(1, id);
		return (Titular) query.getSingleResult();
	}

	@Override
	public List<Titular> getTitularByVolum(Long volumId) {
		Query query = em.createNamedQuery("findByVolum");
		query.setParameter(1, volumId);
		return query.getResultList();
	}

	@Override
	public Volum getVolumById(Long id) {
		Query query = em.createNamedQuery("findVolumById");
		query.setParameter(1, id);
		return (Volum) query.getSingleResult();
	}

	@Override
	public List<Volum> getVolumByNumar(String numar) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void remove(Volum volum) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void remove(Titular titular) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Volum update(Volum volum) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Titular update(Titular titular) {
		// TODO Auto-generated method stub
		return null;
	}
	
}